<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Entering extends CI_Controller {


  public function index()
  {
    $data=array();
    load($this->db,$this->session,$this->load,"entering","index",$data);
  }

  public function add(){
    $data=array();
    load($this->db,$this->session,$this->load,"entering","add",$data); 
  }


  public function edit(){
    $id=$this->input->get('id');
     $data=array();
     $sql="select * from scoreorder where id=$id";
     $query=$this->db->query($sql);
     $order=$query->row();
     $empdepart=$order->empdepart;
     $sql="select name from depart where id=$empdepart";
     $query=$this->db->query($sql);
     $row=$query->row();
     $order->empdepartname=$row->name;
     $sql="select * from order_to_product where order_id=$id";
     $query=$this->db->query($sql);
     $products=$query->result_array();
     foreach ($products as $key => $value) {
       foreach ($value as $key2 => $value) {
         if($key2=="product_id"){
          $sql="select name from point where id=$value";
          $query=$this->db->query($sql);
          $row=$query->row();
          $products[$key]['product_name']=$row->name;
         }
       }
     }
     $data['order']=$order;
     $data['products']=$products;
    load($this->db,$this->session,$this->load,"entering","edit",$data);
  }


  public function del(){
    $id=$this->input->post('id');
    
  }


  public function get_order(){
    $sql="select * from scoreorder where del=0";
    $query=$this->db->query($sql);
    $arr=$query->result_array();
    foreach ($arr as $key => $value) {
      foreach ($value as $key2 => $value) {
        if($key2=="empdepart"){
        $sql="select name from depart where id=$value";
        $query=$this->db->query($sql);
        $row=$query->row();
        $arr[$key]['empdepartname']=$row->name;
         }
      }
    }
    $str=json_encode($arr);
    echo $str;
  }



public function get_score(){
  $arr=array("0"=>array("itemid"=>"1","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"1"=>array("itemid"=>"2","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"2"=>array("itemid"=>"3","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"3"=>array("itemid"=>"4","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"4"=>array("itemid"=>"4","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"5"=>array("itemid"=>"5","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"6"=>array("itemid"=>"6","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"7"=>array("itemid"=>"7","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"8"=>array("itemid"=>"8","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""),"9"=>array("itemid"=>"9","productname"=>"","id"=>"","score"=>"","num"=>"","amount"=>""));
  $str=json_encode($arr);
  echo $str;
}

  public function get_product(){
    $sql="select name,id from `product`";
    $query=$this->db->query($sql);
    $num=$query->num_rows();
    if($num>0){
      $arr=$query->result_array();
    }else{
      $arr;
    }
    $str=json_encode($arr);
    echo $str;
  }



         public function get_emp(){//根据用户ID获取员工列表
          
          $id=$this->input->get('id');
            $departstr=depart_tree_result($this->db,$this->session->userdata('depart'));
          
          if(!empty($id))
            {$sql="select * from employee where category=$id and depart in ($departstr) and del=0";}
          else
            {$sql="select * from employee where depart in ($departstr) and del=0";}
          
          $query=$this->db->query($sql);
          $arr=$query->result_array();
           foreach ($arr as $key => $value) {
              foreach ($value as $key2 => $value) {
                if($key2=="depart"){
                $sql="select name from depart where id=$value";
                $query=$this->db->query($sql);
                $row=$query->row();
                $arr[$key]['departname']=$row->name;
                 }
              }
            }

          $str=json_encode($arr);
          echo $str;
         }


         public function get_pro(){//根据用户ID获取员工列表
          $id=$this->input->get('id');
          if(!empty($id))
            {$sql="select * from point where point_list=$id and del=0";}
          else
            {$sql="select * from point where del=0";}

          $query=$this->db->query($sql);
          $arr=$query->result_array();
          $str=json_encode($arr);
          echo $str;
         }



         public function get_emp_search(){//获取员工搜索
          $key=$this->input->get("key");
          $name=$key;
          $departstr=depart_tree_result($this->db,$this->session->userdata('depart'));

          $sql="select * from employee where `name` like '%$name%' and depart in ($departstr) and del=0 ";
         
          
          $query=$this->db->query($sql);
          $arr=$query->result_array();
          $str=json_encode($arr);
          echo $str;
         }



         public function get_pro_search(){//获取员工搜索
          $key=$this->input->get("key");
          $name=$key;
          $sql="select * from point where name like '%$name%' and del=0 ";
          $query=$this->db->query($sql);
          $arr=$query->result_array();
          $str=json_encode($arr);
          echo $str;
         }



         public function get_emp_category(){
          $result=read_emp_category($this->db,-1);
          $str=json_encode($result);
          echo $str;
         }

           public function get_pro_category(){
          $result=read_pro_category($this->db,-1);
          $str=json_encode($result);
          echo $str;
         }

         public function save(){
          $empid=$this->input->post("empid");
          $result=$this->input->post('result');
          $time=strtotime($this->input->post('time'));
          $total=$this->input->post('total');
          $tmp=0;
          $depart=$this->session->userdata("depart");
          if(empty($time)){
            $time=time();
          }
         //*1-sdfdsfs-9-60.0-1-60.00*2-sdfdsfs-9-60.0-1-60.00*3-sdfdsfs-9-60.0-1-60.00
         $sql="select * from employee where id=$empid";
         $query=$this->db->query($sql);
         $emp=$query->row();
         $arr=array("empid"=>$empid,"empname"=>$emp->name,"empdepart"=>$depart,"emptelphone"=>$emp->telphone,"time"=>$time,"sumtotal"=>$total,"del"=>0,);
          if($this->db->insert('scoreorder',$arr)){
           $order_id=$this->db->insert_id();
            $result=substr($result, 1);
             $a=explode("*", $result);
             foreach ($a as $key => $value) {
               $b=explode("-", $value);
               $arr=array("order_id"=>$order_id,"product_id"=>$b[2],"product_num"=>$b[4],"score"=>$b[3],"total"=>$b[5],"empid"=>$empid,"time"=>$time);
               if($this->db->insert('order_to_product',$arr)){

               }else{
                $tmp=1;
               }
               
             }
          }else{
            $tmp=1;
          }

        if($tmp==0){
          echo "success";
        }

         }




          public function editsave(){
          $empid=$this->input->post("empid");
          $result=$this->input->post('result');
          $time=strtotime($this->input->post('time'));
          $total=$this->input->post('total');
          $order_id=$this->input->post('order_id');
          $tmp=0;
          $depart=$this->session->userdata("depart");
          if(empty($time)){
            $time=time();
          }
         //*1-sdfdsfs-9-60.0-1-60.00*2-sdfdsfs-9-60.0-1-60.00*3-sdfdsfs-9-60.0-1-60.00

         
         $sql="select * from employee where id=$empid";
         $query=$this->db->query($sql);
         $emp=$query->row();
         $arr=array("empid"=>$empid,"empname"=>$emp->name,"empdepart"=>$depart,"emptelphone"=>$emp->telphone,"sumtotal"=>$total,"del"=>0,);
           $this->db->where('id',$order_id);
          if($this->db->update('scoreorder',$arr)){
          
            $sql="delete  from order_to_product where order_id=$order_id";
            if($this->db->query($sql)){
                

                 $result=substr($result, 1);
                   $a=explode("*", $result);
                   foreach ($a as $key => $value) {
                     $b=explode("-", $value);
                     $arr=array("order_id"=>$order_id,"product_id"=>$b[2],"product_num"=>$b[4],"score"=>$b[3],"total"=>$b[5],"empid"=>$empid);
                     if($this->db->insert('order_to_product',$arr)){

                     }else{
                      $tmp=1;
                     }
                     
                   }


            }else{
              $tmp=1;
            }
           




          }else{
            $tmp=1;
          }

        if($tmp==0){
          echo "success";
        }

         }


}
